package api

import (
	"pids-cloud-server/app/common"
	"pids-cloud-server/constants"

	"github.com/gin-gonic/gin"
)

// GetAllAlarmStatus 获取所有告警状态信息
// @Summary 获取所有告警状态信息
// @Description 获取所有激活告警状态的主机和关闭告警状态的主机数据
// @Tags 告警状态
// @Accept json
// @Produce json
// @Success 200 {object} common.Result
// @Router /alarm/status [get]
func GetAllAlarmStatus(c *gin.Context) {
	// 获取所有主机的告警状态
	activeHosts, inactiveHosts := constants.GlobalAlarmManager.GetAllHostsWithAlarmStatus()

	// 将主机告警数据转换为可以直接返回的格式
	activeHostsData := make([]map[string]interface{}, 0, len(activeHosts))
	inactiveHostsData := make([]map[string]interface{}, 0, len(inactiveHosts))

	// 处理活跃告警主机数据
	for _, host := range activeHosts {
		// 转换告警信息为可序列化的格式
		alarms := make([]map[string]interface{}, 0, len(host.Alarms))
		for alarmType, alarm := range host.Alarms {
			alarms = append(alarms, map[string]interface{}{
				"type":       alarmType,
				"message":    alarm.Detail,
				"status":     alarm.Status,
				"created_at": alarm.CreateTime,
				"host_ip":    alarm.HostIP,
				"flag":       alarm.Flag,
			})
		}

		hostData := map[string]interface{}{
			"host_ip":      host.HostIP,
			"status":       host.Status,
			"last_updated": host.LastUpdated,
			"alarm_count":  host.AlarmCount,
			"alarms":       alarms,
		}
		activeHostsData = append(activeHostsData, hostData)
	}

	// 处理非活跃告警主机数据
	for _, host := range inactiveHosts {
		hostData := map[string]interface{}{
			"host_ip":      host.HostIP,
			"status":       host.Status,
			"last_updated": host.LastUpdated,
			"alarm_count":  host.AlarmCount,
		}
		inactiveHostsData = append(inactiveHostsData, hostData)
	}

	// 返回结果
	result := map[string]interface{}{
		"active_hosts":   activeHostsData,
		"inactive_hosts": inactiveHostsData,
		"counts": map[string]int{
			"active":   len(activeHostsData),
			"inactive": len(inactiveHostsData),
			"total":    len(activeHostsData) + len(inactiveHostsData),
		},
	}

	common.SuccessResponse(c, result)
}

// GetActiveAlarmHosts 获取有活跃告警的主机列表
// @Summary 获取有活跃告警的主机列表
// @Description 获取所有有活跃告警的主机列表
// @Tags 告警状态
// @Accept json
// @Produce json
// @Success 200 {object} common.Result
// @Router /alarm/active [get]
func GetActiveAlarmHosts(c *gin.Context) {
	activeHosts, _ := constants.GlobalAlarmManager.GetAllHostsWithAlarmStatus()

	// 将主机告警数据转换为可以直接返回的格式
	activeHostsData := make([]map[string]interface{}, 0, len(activeHosts))

	// 处理活跃告警主机数据
	for _, host := range activeHosts {
		// 转换告警信息为可序列化的格式
		alarms := make([]map[string]interface{}, 0, len(host.Alarms))
		for alarmType, alarm := range host.Alarms {
			alarms = append(alarms, map[string]interface{}{
				"type":       alarmType,
				"message":    alarm.Detail,
				"status":     alarm.Status,
				"created_at": alarm.CreateTime,
				"host_ip":    alarm.HostIP,
				"flag":       alarm.Flag,
			})
		}

		hostData := map[string]interface{}{
			"host_ip":      host.HostIP,
			"status":       host.Status,
			"last_updated": host.LastUpdated,
			"alarm_count":  host.AlarmCount,
			"alarms":       alarms,
		}
		activeHostsData = append(activeHostsData, hostData)
	}

	result := map[string]interface{}{
		"hosts": activeHostsData,
		"count": len(activeHostsData),
	}

	common.SuccessResponse(c, result)
}

// GetInactiveAlarmHosts 获取没有活跃告警的主机列表
// @Summary 获取没有活跃告警的主机列表
// @Description 获取所有没有活跃告警的主机列表
// @Tags 告警状态
// @Accept json
// @Produce json
// @Success 200 {object} common.Result
// @Router /alarm/inactive [get]
func GetInactiveAlarmHosts(c *gin.Context) {
	_, inactiveHosts := constants.GlobalAlarmManager.GetAllHostsWithAlarmStatus()

	// 将主机告警数据转换为可以直接返回的格式
	inactiveHostsData := make([]map[string]interface{}, 0, len(inactiveHosts))

	// 处理非活跃告警主机数据
	for _, host := range inactiveHosts {
		hostData := map[string]interface{}{
			"host_ip":      host.HostIP,
			"status":       host.Status,
			"last_updated": host.LastUpdated,
			"alarm_count":  host.AlarmCount,
		}
		inactiveHostsData = append(inactiveHostsData, hostData)
	}

	result := map[string]interface{}{
		"hosts": inactiveHostsData,
		"count": len(inactiveHostsData),
	}

	common.SuccessResponse(c, result)
}
